package myself.study.network.tcp.examples.ex04;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import org.junit.Test;

/**
* @Name: ImageUploadClient
* @Description: 图片上传客户端
* @Author: 李超	lc_jack@foxmail.com
* @CreateDate: 2017-1-12 下午10:20:24 
* @Version: V1.0
 */
public class ImageUploadClient {

	@Test
	public void client() throws UnknownHostException, IOException {
		//创建客户端Socket对象
		Socket client = new Socket("127.0.0.1", 12345) ;
		//创建文件字节输入流，并包装成高效字节输入流
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\111.jpg")) ;
		//获取字节输出流，并包装成高效字节输出流
		BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()) ;
		//上传文件
		byte[] buf = new byte[1024] ;
		int len = 0 ;
		while((len = bis.read(buf)) != -1) {
			bos.write(buf, 0, len) ;
			bos.flush() ;
		}
		//设置上传完成结束标志
		client.shutdownOutput() ;
		//接收服务端响应结果
		InputStream inputStream = client.getInputStream() ;
		byte[] buf2 = new byte[1024] ;
		int len2 = inputStream.read(buf2) ;
		String status = new String(buf2, 0, len2);
		System.out.println(status);
		//释放资源
		bis.close() ;
		client.close() ;
	}
	
}











